<?php

namespace brand\page;

use brand\Brand;
use brand\logic\BrandHelper;

class Page
{


    public static $curdName;
    public static $curdConfig;
    public static $config;
    //新增Url
    public static $add_url;
    //编辑Url
    public static $edit_url;
    //删除Url
    public static $delete_url;
    //表格编辑url
    public static $update_url;
    //搜索数据页面
    public static $table_data_url;
    //导出页面
    public static $exportUrl;
    //标题
    public static $title = "";
    //小标题
    public static $describe = "";
    //分页数量
    public static $page_size;
    //默认顶部按钮
    public static $header_buttons;
    //默认右侧按钮
    public static $right_buttons;
    //索引
    public static $pk ='id';
    //自定义隐藏编辑按钮字段
    public static $visible_edit = "";
    //自定义隐藏删除按钮
    public static $visible_delete = "";
    //混入js
    public static $mixinJs = "";

    //自定义按钮
    public static $custom_button = [];
    public static $custom_button_header = [];

    public static $SurfaceTable;

    public function __construct($curdName = "")
    {
        self::$curdName = $curdName ?: BrandHelper::getCurdNameByControllerName();
    }

    public static function buttonProps($text = '', $type='primary', $size='mini') : array
    {
        return [
            'text' => $text,
            'doneRefresh' => true,
            'prop'  =>  [
                'type'=>$type,
                'size'=>$size
            ],
        ];
    }




    /**
     * @return mixed
     */
    public static function getConfig()
    {
        return self::$config;
    }

    /**
     * @param mixed $config
     */
    public static function setConfig($config)
    {
        self::$config = $config;
    }

    public static function getStatistics()
    {
        return self::$config['statistics'] ?? [];
    }




    public static function setPageConfig()
    {
        $pageConfig = Page::getConfig();
        foreach ($pageConfig as $key => $item) {
            $action = 'set_' . $key;
            if (method_exists(self::class,$action)){
                self::$action($item);
            }
        }
    }

    /**
     * @param string $title
     */
    public static function set_title(string $title): void
    {
        self::$title = $title;
    }

    /**
     * @param string $describe
     */
    public static function set_describe(string $describe): void
    {
        self::$describe = $describe;
    }


    /**
     * @param mixed $page_size
     */
    public static function set_page_size($page_size): void
    {
        self::$page_size = $page_size;
    }

    /**
     * @param mixed $header_buttons
     */
    public static function set_header_buttons($header_buttons): void
    {
        self::$header_buttons = $header_buttons;
    }

    /**
     * @param mixed $right_buttons
     */
    public static function set_right_buttons($right_buttons): void
    {
        self::$right_buttons = $right_buttons;
    }


    public static function set_custom_header_buttons($custom_header_buttons)
    {
        foreach ($custom_header_buttons as $button) {
            self::$custom_button_header[] = Brand::Builder()
                ->build_button('',$button['icon'] ?? '',$button['event'] ?? 'page')
                ->button_confirm($button['confirm'] ?? '')
                ->button_url(request()->root() . $button['url'] ?? '')
                ->button_props(self::buttonProps($button['title'] ?? ''))
                ->build();
        }
    }

    public static function set_custom_right_buttons($custom_right_buttons)
    {

        foreach ($custom_right_buttons as $button) {
            $button_data = [];
            $page_data = $button['data'] ?? [];
            foreach ($page_data as $page_datum) {
                if (!isset($page_datum['key']) || !$page_datum['key']) continue;
                if (isset($page_datum['value']) && $page_datum['value']){
                    $button_data[$page_datum['key']] = $page_datum['value'];
                }else{
                    $button_data[] = $page_datum['key'];
                }
            }
            if (!$button_data) $button_data = [self::$pk];
            self::$custom_button[] = Brand::Builder()
                ->build_button($button['title'] ?? '',$button['icon'] ?? '',$button['event'] ?? 'page')
                ->button_confirm($button['confirm'] ?? '')
                ->button_url(request()->root() . $button['url'] ?? '')
                ->button_visible($button['visible'] ?? '')
                ->button_data($button_data)
                ->build();
        }
    }

    /**
     * 设置新增页面url
     * @param $url
     * @return void
     */
    public static function setAddUrl($url = 'add')
    {
        self::$add_url = url($url,request()->get())->build();
    }

    /**
     * 设置修改页面url
     * @param $url
     * @return void
     */
    public static function setEditUrl($url = 'edit')
    {
        self::$edit_url = url($url,request()->get())->build();
    }

    /**
     * 设置删除页面url
     * @param $url
     * @return void
     */

    public static function setDeleteUrl($url = 'del')
    {
        self::$delete_url = url($url)->build();
    }

    /**
     * 设置更新url
     * @param $url
     * @return void
     */
    public static function setUpdateUrl($url = 'edit')
    {
        self::$update_url = url($url,['brand_update'=>1])->build();
    }

    public static function setTableDataUrl($url = 'index')
    {
        self::$table_data_url = url($url,request()->get())->build();
    }

    public static function setExportUrl($url = 'export')
    {
        self::$exportUrl = url($url)->build();
    }





}